$(function () {
    layui.use(['rate', 'layer', 'form', 'flow', 'util'], function () {
        var layer = layui.layer, form = layui.form, flow = layui.flow, util = layui.util;
        util.fixbar({
            click: function (type) {
            }
        });
        categoryList()
        downloadList(flow)
        $('.course-option .option-text-items').on('click', '.item-text', function () {
            $('#paperList').empty() // 先清空之前的数据再查询
            if ($(this).attr('data-label') == 'category_id') {
                categoryId = $(this).attr('data-category-id')
            } else if ($(this).attr('data-label') == 'level') {
                level = $(this).attr('data-level')
            }
            downloadList(flow)
        })
        form.render()
    });
    /*jQuery动态添加元素hover事件改成mouseenter*/
    $('.paper-lists .exam-lists').on('mouseenter', '.exam-item', function () {
        $(this).find('.model').fadeIn(100)
    })
    $('.paper-lists .exam-lists').on('mouseleave', '.exam-item', function () {
        $(this).find('.model').fadeOut(100)
    })
    // 头部查询条件鼠标选中时样式切换
    $('.option-text-items').on('click', 'span', function () {
        $(this).siblings().removeClass('item-click') // 删除所有兄弟节点
        $(this).addClass('item-click')
    })
})

/**
 * 加载类目列表
 */
function categoryList() {
    $.ajax({
        url: '/category/queryList',
        type: "POST",
        dataType: 'json',
        success: function (res) {
            layui.each(res.data, function (index, item) {
                let div = '<span class="item-text" data-category-id="' + item.id + '" data-label="category_id">' + item.categoryName + '</span>'
                $('.category-option-item').append(div)
            });
        }
    });
}

/**
 * 信息流方式加载所有试卷
 * @param flow
 */
let categoryId = '0'

function downloadList(flow) {
    // 信息流加载列表数据
    flow.load({
        elem: '#dataList', //指定列表容器
        isAuto: false,
        done: function (page, next) { //到达临界点（默认滚动触发），触发下一页
            let lis = [];
            let paperEntity = {pageNum: page, categoryId: categoryId}
            $.ajax({
                url: '/data/queryList',
                type: "post",
                dataType: 'json',  // 返回数据的数据类型json
                data: {dataEntity: JSON.stringify(paperEntity)},  //传送的数据
                success: function (res) {
                    layui.each(res.data, function (index, item) {
                        let star = ''
                        for (let i = 0; i < item.level; i++) {
                            star += '<i class="star"></i>'
                        }
                        let html = '<div class="exam-item layui-anim layui-anim-up">\n' +
                            '                    <h3 class="h"><span>' + item.title + '</span></h3>\n' +
                            '                    <div class="over-text"><span>下载次数：</span>' + item.downNum + '</div>\n' +
                            '                    <div class="create-time">\n' +
                            '                        <span>标签：<span class="icon_label">' + (item.categoryName === null ? '暂无分类' : item.categoryName) + '</span></span>\n' +
                            '                        <span>发布时间：<span>' + dateFormat(item.createTime) + '</span></span>\n' +
                            '                    </div>\n' +
                            '                    <div class="model">\n' +
                            '                        <div class="btn">\n' +
                            '                            <button class="layui-btn layui-btn-normal start-btn" onclick="downloadData(' + item.id + ')">下载资料</button>\n' +
                            '                        </div>\n' +
                            '                    </div>\n' +
                            '                </div>';
                        lis.push(html);
                    });
                    next(lis.join(''), page <= res.data.length);
                    // 记录数
                    $('.paper-lists .list-tab .right-bar__text').html('共 <strong>' + res.count + '</strong> 个课程')
                }
            });
        }
    });
}


/**
 * 打开一套试卷
 * @param id
 */
function downloadData(id) {
    location.href = '/data/download/' + id
}